﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace ASBCC.Workflow.K2Manager
{
    public class K2AuditDestination
    {
        private List<ActivityDestination> _activityDestinationList = new List<ActivityDestination>();
        public List<ActivityDestination> ActivityDestinationList { get { return this._activityDestinationList; } }

        public K2AuditDestination()
        {
        }

        public K2AuditDestination(string activityName, int[] userArr)
        {
            _activityDestinationList.Add(new ActivityDestination(activityName, userArr));
        }

        public void AddActivityDestination(string activityName, int[] userArr)
        {
            _activityDestinationList.Add(new ActivityDestination(activityName, userArr));
        }

        public IEnumerator GetEnumerator()
        {
            IEnumerator enumtor = _activityDestinationList as IEnumerator;
            return enumtor;
        }

        public string ExportText()
        {
            List<string> list = new List<string>();
            foreach (ActivityDestination activityDestination in ActivityDestinationList)
            {
                list.Add(activityDestination.ExportText());
            }
            return string.Join(Consts.SEPARATOR_AUDIT_DESTINATION.ToString(), list.ToArray());
        }

        public static K2AuditDestination LoadXml(string xml)
        {
            if (string.IsNullOrEmpty(xml)) return null;
            K2AuditDestination auditDestination = new K2AuditDestination();
            
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNodeList activityNodeList = xmlDoc.DocumentElement.SelectNodes("/auditDestination/activity");
            foreach (XmlNode activityNode in activityNodeList)
            {
                string activityName = activityNode.Attributes[0].Value;
                List<int> list = new List<int>();
                XmlNodeList userNodeList = activityNode.SelectNodes("userList/user");
                foreach (XmlNode userNode in userNodeList)
                {
                    list.Add(Convert.ToInt32(userNode.Value));
                }
                auditDestination.AddActivityDestination(activityName, list.ToArray());
            }

            return auditDestination;
        }

        public static K2AuditDestination LoadText(string text)
        {
            if (string.IsNullOrEmpty(text)) return null;
            K2AuditDestination auditDestination = new K2AuditDestination();

            string[] activityDestinationItems = text.Split(new char[] { Consts.SEPARATOR_AUDIT_DESTINATION }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string activityDestinationItem in activityDestinationItems)
            {
                string item = activityDestinationItem.Trim();
                string[] subItems = item.Split(new char[] { Consts.SEPARATOR_ACTIVITY_DESTINATION }, StringSplitOptions.None); //ActivityName允许为空
                if (subItems.Length >= 1)
                {
                    string activityName = subItems[0].Trim();
                    string users = string.Empty;
                    if (subItems.Length >= 2) users = subItems[1].Trim();
                    string[] userArr = users.Split(new char[] { Consts.SEPARATOR_USER }, StringSplitOptions.RemoveEmptyEntries);
                    List<int> userList = new List<int>();
                    foreach (string user in userArr)
                    {
                        userList.Add(Convert.ToInt32(user));
                    }
                    auditDestination.AddActivityDestination(activityName, userList.ToArray());
                }
            }

            return auditDestination;
        }

        //Added by GJ 2011-6-15
        public static string GetCcuser(string ccUser)
        {
            string[] subItems = ccUser.Split(new char[] { Consts.SEPARATOR_ACTIVITY_DESTINATION }, StringSplitOptions.None);

            if (subItems.Length >= 3)
            {
                return subItems[2].Trim();
            }
            return string.Empty;
        }

        public class ActivityDestination
        {
            private string _activityName;
            public string ActivityName { get { return _activityName; } }

            private int[] _userArr;
            public int[] UserArr { get { return _userArr; } }

            public ActivityDestination(string activityName, int[] userArr)
            {
                _activityName = activityName;
                _userArr = userArr;
            }

            public string ExportXmlFieldValue()
            {
                StringBuilder sb = new StringBuilder();
                using (XmlWriter xmlWriter = XmlWriter.Create(sb))
                {
                    xmlWriter.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
                    xmlWriter.WriteStartElement("auditDestination");
                    xmlWriter.WriteStartElement("activity");
                    xmlWriter.WriteAttributeString("name", _activityName);
                    xmlWriter.WriteStartElement("userList");
                    foreach (int user in _userArr)
                    {
                        xmlWriter.WriteElementString("user", user.ToString());
                    }
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();
                }
                return sb.ToString();
            }

            public string ExportText()
            {
                List<string> userList = new List<string>();
                if (_userArr != null)
                {
                    foreach (int user in _userArr)
                    {
                        userList.Add(user.ToString());
                    }
                }
                string users = string.Join(Consts.SEPARATOR_USER.ToString(), userList.ToArray());
                return string.Join(Consts.SEPARATOR_ACTIVITY_DESTINATION.ToString(), new string[] { _activityName, users });
            }
        }

        public class Consts
        {
            public const char SEPARATOR_USER = ';';
            public const char SEPARATOR_ACTIVITY_DESTINATION = '|';
            public const char SEPARATOR_AUDIT_DESTINATION = '$'; 
        }
    }
}
